<?php
	include "./application/core/session.php";
	
	include DIR_MODEL		. "personalInformationMod.php";
	include DIR_CONTROLLER	. "personalInformationCon.php";
	
	include DIR_MODEL		. "studentInformationMod.php";
	include DIR_CONTROLLER	. "studentInformationCon.php";
	
	include DIR_MODEL		. "departmentSubjectCourseMod.php";
	include DIR_CONTROLLER	. "departmentSubjectCourseCon.php";
	
	include DIR_MODEL		. "announcementMod.php";
	include DIR_CONTROLLER	. "announcementCon.php";
	
	include DIR_MODEL		. "globalMod.php";
	include DIR_CONTROLLER	. "globalCon.php";
	
	$ANNOUNCE 	= new WS_AnnouncementCon();
	$PI  		= new WS_PersonalInformationCon();
	$DSC 		= new WS_DepartmentSubjectCourseCon();
	$STUD 		= new WS_StudentInformationCon();
	$GLOBAL		= new WS_GlobalCon();

	$getVar = $_POST['AJAXPost'];
	
	if (!empty($getVar)) {
		
		switch($getVar) {
			
			case 'InputGradeRequisite'	: 	$requisite = $DSC->getInputGradeRequisite($_POST['strSubjLinkerID'], $_POST['strSemLinkerActiveID'], $_POST['stud_id'], $_POST['take_course']);
											$countReq = count($requisite);
											if ( $countReq > 0)
												echo json_encode($requisite);
											else echo json_encode(0);
											exit();
											break;
			
			case 'AddSubjEnrolled'	: 	$strArray = array('stud_id' 	=> $_POST['stud_id'],
														  'subj_id' 	=> $_POST['subj_id'],
														  'course_id' 	=> $_POST['course_id'],
														  'sem_id' 		=> $_POST['sem_id'],
														  'year_id' 	=> $_POST['year_id']);
			
										$subj_taken 	= $_POST['subj_taken'];
			
										$STUD->insertStudentTransferyGrades($strArray, $_POST['subj_taken']);
										
										break;
											
																					   
			$stud_id 		= escapestring($strArray['stud_id']);
			$subj_id 		= escapestring($strArray['subj_id']);
			$course_id 		= escapestring($strArray['course_id']);
			$sem_id 		= escapestring($strArray['sem_id']);
			$year_id 		= escapestring($strArray['year_id']);
			$grades 		= escapestring($strArray['grades']);
			$subj_taken 	= escapestring($activeSubjTaken);
			
			case 'StudentNextSubjectDetails'	: 	$requisite = $DSC->getStudentNextSubjectDetails($_POST['strSubjLinkerID']);
													$countReq = count($requisite);
													if ( $countReq > 0)
														echo json_encode($requisite);
													else
														echo json_encode(0);
													exit();
													break;
			
			case 'UpdateStudentEnrolled' : 	$listing = $STUD->getStudentSubjectTakenListSeperateBySem($_POST['stud_id'], $_POST['course_id'], $_POST['sem_linker_id']);
											$countReq = count($listing);
											if ( $countReq > 0)
												echo json_encode($listing);
											else
												echo json_encode(0);
											exit();
											break;
			
			case 'ScheduleSubject' : 	$scheduleTimePerSubj = $STUD->getScheduleTimePerSubj($_POST['subj_id']);
										echo json_encode($scheduleTimePerSubj); exit(); break;
									
			case 'DeleteSubjEnrolled'	: 	$deleteSubjEnrolled = $STUD->deleteSubjEnrolled($_POST['stud_subj_linker_id']); echo json_encode($deleteSubjEnrolled); exit(); break;
								
			case 'DeleteSubjEnrolledGrades'	: 	$STUD->deleteSubjEnrolledGrades($_POST['stud_subj_linker_id']); break;
			
			case 'coRequisite'	: 	$requisite = $DSC->requisite($_POST['strSubjLinkerID']);
									$countReq = count($requisite);
									if ( $countReq > 0)
										echo json_encode($requisite);
									else
										echo json_encode(0);
									exit();
									break;
									
			case 'getSemesterIrregular'	: 	$semester = $DSC->getSemesterIrregular($_POST['strCourseID'], $_POST['studID']);
											echo json_encode($semester);
											exit();
											break;
									
			case 'preRequisite'	: 	$semester = $DSC->getSemesterLinker($_POST['strCourseID']);
									echo json_encode($semester);
									exit();
									break;
									
			case 'preRequisiteEnrolled'	: 	$semester = $DSC->getSemesterLinkerEnrolled($_POST['strCourseID'], $_POST['strStudID']);
											echo json_encode($semester);
											exit();
											break;
								
			case 'deletePicture': 	if (file_exists($_POST['fileName'])) {
										unlink($_POST['fileName']);
									}														break;
								
			case 'deleteAccount': 	$PI->deletePersonalInformation($_POST['strID']);		break;
								
			case 'deleteAnnouncement': 	$ANNOUNCE->deleteAnnouncement($_POST['strID']);		break;
									
			case 'deleteCourse': 	$DSC->deleteCourse($_POST['strID']);					break;
								
			case 'deleteSubject': 	$DSC->deleteSubject($_POST['strID']);					break;
								
			case 'insertSemLinker': $DSC->insertSemLinker($_POST['strSem'], $_POST['strCourse'], $_POST['strHead'], $_POST['strYear']);				break;

			case 'deleteSemLinker': $DSC->deleteSemLinker($_POST['strSem']);				break;
			
			case 'updateSemLinker': $DSC->updateSemLinker($_POST['strSem']);				break;

			case 'semLinkerNoteSave': $DSC->udpateSaveSemLinkerNote($_POST['strSemLinkerID'], $_POST['strValue']);	break;

			case 'courseDepartment': $courses = $DSC->getListCourses($_POST['strDepartmentID']);
									 echo json_encode($courses); exit(); break;

			case 'SaveTransferyGrades': $strArray = array('stud_id'	=> $_POST['stud_id'],
														   'subj_id'	=> $_POST['subj_id'],
														   'course_id'	=> $_POST['course_id'],
														   'sem_id'		=> $_POST['sem_id'],
														   'year_id'	=> $_POST['year_id'],
														   'grades'		=> $_POST['grades']);
														   
										$strStudSubjID = $_POST['stud_subj_id'];
										
										$returnData = $STUD->studentTransferyGrades($strArray, $_POST['grades'], $strStudSubjID);
										
										echo json_encode($returnData); exit();
										
										break;

			case 'SaveStudentGrades': 	$returnData = $STUD->studentGrades($_POST['grades'], $_POST['stud_subj_id']);
										
										echo json_encode($returnData); exit();
										
										break;

			case 'StudentGradeSubjListing': $returnData = $STUD->getStudentSubjectGradesList($_POST['stud_id'], $_POST['course_id']);
											
											echo json_encode($returnData); exit();
											
											break;

			case 'Semester': $returnSemester = $DSC->getSemesterLabel($_POST['sem_id']);
											
							echo json_encode(array('sem_desc' => $returnSemester['sem_desc'])); exit();
							
							break;

			case 'GradeSubjEnrollSemester': $studID 	 = $_POST['stud_id'];
											$courseID 	 = $_POST['course_id'];
											$semLinkerID = $_POST['sem_linker_id'];
											
											$studTakenSubjGrades = $STUD->getStudentSubjectGradesListWithOutSemIdentities($studID, $courseID);
											
											if(!empty($studTakenSubjGrades)) {
												$studTakenSubj = '';
												$studTakenSem = array();
												foreach($studTakenSubjGrades as $studTakenSubjGradesValue) {
													$studTakenSubj .= $studTakenSubjGradesValue['subj_idPK'] . ',';
													$studTakenSem[] = $studTakenSubjGradesValue['sem_idFK'];
												}
												$studTakenSubjTrim = rtrim($studTakenSubj, ',');
											}
											
											if(!empty($studTakenSem)) {
												$strStusSubjTakenUnique = implode(',', array_unique($studTakenSem));
											}
											
											$studSubjUntakenPerSemester = $STUD->getStudSubjUntakenPerSemester($studTakenSubjTrim, $strStusSubjTakenUnique, $semLinkerID);
											
											if(!empty($studSubjUntakenPerSemester)) {
												foreach($studSubjUntakenPerSemester as $studSubjUntakenPerSemesterValue) {
													$resUntaken[$studSubjUntakenPerSemesterValue['sem_linker_idFK']][] = $studSubjUntakenPerSemesterValue;
												}
											}
											
											echo json_encode($resUntaken); exit();
											
											break;

			case 'requisiteSubjCourse': $courseID 	= $_POST['course_id'];
										
										$subjRequisite = $DSC->getSubjCourseRequisite($courseID);
														
										echo json_encode($subjRequisite); exit();
										
										break;

			case 'gradeTotalUnitsPerSemester':  $semLinkerID 	= $_POST['sem_linker_id'];
										
												$totalUnitsPerSemesterLinker = $DSC->getTotalUnitsPerSemesterLinker($semLinkerID);
																
												echo json_encode(array('total_units' => $totalUnitsPerSemesterLinker)); exit();
												
												break;
												
			case 'semesterTransferyMoerSubj'	: 	$semester = $DSC->getSemesterLinker($_POST['strCourseID'], rtrim($_POST['strActiveSem'],','));
													echo json_encode($semester);
													exit();
													break;
												
			case 'GetFeeList'	: 	$feeList = $DSC->getSemFeesLinkerListing($_POST['sem_linker_id']);
									echo json_encode($feeList); exit();
									break;	
									
			case 'DeleteSemesterFeesActive'	: 	$DSC->deleteSemesterFeesActive($_POST['sem_fees_linker_id']); break;
												
			case 'GetTotalUnits'	: 	$totalUnits = $DSC->getTotalUnitsPerSemester($_POST['sem_linker_id']);
										echo json_encode($totalUnits); exit();
										break;
												
			case 'GetSemFeeLinkerID'	: 	$semLinkerFeeSemesterLinker = $DSC->getSemLinkerFeeSemesterLinker($_POST['sem_fee_linker_id']);
											echo json_encode($semLinkerFeeSemesterLinker); exit();
											break;
												
			case 'PaymentHistory'	: 	$studentPaymentHistory = $DSC->getStudentPaymentHistory($_POST['active_sem'], $_POST['stud_id'], $_POST['course_id']);
										echo json_encode($studentPaymentHistory); exit();
										break;
												
			case 'SavePayment'		: 	$DSC->insertStudentPaymentHistory($_POST['active_sem'], $_POST['stud_id'], $_POST['course_id'], $_POST['amount'], $_POST['method']);
										break;
												
			case 'StudSectionAutoGenerate'	: 	$semLinker 	= $_POST['sem_linker_id'];
												$courseID 	= $_POST['course_id'];
												$sectionID	= $_POST['section_id'];
												$yearID		= $_POST['year_id'];
			
												$studs 	= $STUD->getStudentEnrolled($semLinker, $courseID, $yearID);
												$secs 	= $STUD->getDefaultSections($courseID, $sectionID);
												$noStud	= $secs[0]['no_stud'];
												
												if (!empty($studs)) {
													$count = 1;
													foreach($studs as $studsVal) {
														$studEnrolledSection = $STUD->getStudentSectionEnrolled($studsVal['stud_sem_idPK']);
														if ( $studEnrolledSection['stud_sec'] <= 0 ) {
															if ( $count <= $noStud )
																$STUD->insertStudentSectionSemester($studsVal['stud_sem_idPK'], $courseID, $sectionID, $semLinker);
															else 
																exit();
															$count++;
														}
													}
												}
												
												break;
												
			case 'SectionAutoGenerate' : $STUD->insertDefaultSections($_POST['section_desc'], $_POST['course_id'], $_POST['sem_id'], $_POST['no_stud']); exit(); break;
			
			case 'ProfSubjects' 		: $DSC->insertUpdateProfSubj($_POST['subj_id'], $_POST['prof_id'], $_POST['action_value'], $_POST['year_id']); exit(); break;
			
			case 'DeleteStudentSection'	: $STUD->emptyStudSection($_POST['section_id']); break;
			
			case 'DeleteSection'		: $STUD->updateDeptSection($_POST['section_id']);		break;
			
			case 'DeleteRoom'			: $DSC->updateDeptRooms($_POST['room_id']);		break;
			
			case 'SaveSchedulesRemoveTemp'		: $STUD->saveSchedulesRemoveTemp($_POST['user_id']); break;
			
			case 'DeleteTimeSchedule'		: $STUD->deleteTimeSchedule($_POST['prof_subj_sched_id']); break;
			
			case 'InputGradeRequisiteGradeIfDrop'	: $grade = $DSC->inputGradeRequisiteGradeIfDrop($_POST['stud_subj_linker_id'], $_POST['stud_sem_linker_id'], $_POST['stud_id']);
														echo json_encode($grade); exit(); break;	
			
			
			case 'SubjectSchedulingSelected'	: $subjScheduling = $STUD->getProfSubjList($_POST['prof_id'], $_POST['prof_subj_id']);
												 
												  if ( !empty($_POST['prof_subj_id']) ) {
													  $labHrs	= 0;
													  $lecHr	= 0;
													  $lecLabHr = 0;
													  $profSubjActive = $STUD->getProfSubjActive($_POST['prof_subj_id'], $_POST['section_id']);
													 
													  $timeYear = $STUD->getTimeSchedule('sched_time', $_POST['year_id'], '', $_POST['stid']);
													  
															$totalUnits	= $subjScheduling[0]['total_units']; 
															$labHrs 	= $subjScheduling[0]['lab_units'];
															$totalHrs	= $subjScheduling[0]['total_hrs']; 
															$lecHr 		= $subjScheduling[0]['lec_units'];
															$lecLabHr 	= $subjScheduling[0]['lec_lab_units'];
															 
															$labHr 		= ( $totalHrs > $totalUnits ) ? ($totalHrs - $lecHr) : $labHrs; 
															
															if ( !empty($profSubjActive) ) {
																foreach($profSubjActive as $profSubjActiveValue) {
																	if ( $profSubjActiveValue['lec_lab_both'] == 0 ) $lecHr 	-= round($profSubjActiveValue['sched_time'], 1);
																	if ( $profSubjActiveValue['lec_lab_both'] == 1 ) $labHr 	-= round($profSubjActiveValue['sched_time'], 1);
																	if ( $profSubjActiveValue['lec_lab_both'] == 2 ) $lecLabHr 	-= round($profSubjActiveValue['sched_time'], 1);
																}
															}
															
														echo json_encode(array('lab' => $labHr, 'lec' => $lecHr, 'lec_lab' => $lecLabHr, 'hrs' => $timeYear[0])); exit(); break;	
												   }
												   else {
														echo json_encode(array('lab' => 0, 'lec' => 0, 'lec_lab' => 0)); exit(); break;	  
												   }
			
			case 'ActivateStudent'			: $STUD->activateStudent($_POST['stud_id']); break;
			
			case 'ActivateStudentSemester'	: $STUD->ActivateStudentSemester($_POST['stud_sem_id']); break;
			
			case 'ChangeTimeScheduleActive'	: $STUD->changeTimeScheduleActive($_POST['year_id']); break;
			
			case 'ActivateCreditStudent'	: $STUD->insertStudentSemesterActive($_POST['stud_id'], $_POST['course_id'], $_POST['first_sem_id'], $_POST['active_year']); break;
			
			case 'StudentChangeSection'	: $studentChangeSectionID = $STUD->insertStudentChangeSection($_POST['stud_id'],
																									  $_POST['course_id'],
																									  $_POST['sem_linker_id'],
																									  $_POST['year_id'],
																									  $_POST['current_section_id'],
																									  $_POST['change_section_id']);
																									  break;
			
			/*case 'IrregularSetSchedule'	: $strArray = array('stud_id' 			 => $_POST['stud_id'],
															'course_id' 		 => $_POST['course_id'],
															'sem_linker_id' 	 => $_POST['sem_linker_id'],
															'year_id' 			 => $_POST['year_id'],
															'subj_id' 			 => $_POST['subj_id'],
															'prof_subj_sched_id' => $_POST['prof_subj_sched_id']);
											$STUD->IrregularSetScheduleStudent($strArray, 'P', 'Y', true);
											break;*/
			case 'IrregularSetSchedule'	: $strArray = array('stud_id' 			 => $_POST['stud_id'],
															'course_id' 		 => $_POST['course_id'],
															'sem_linker_id' 	 => $_POST['sem_linker_id'],
															'year_id' 			 => $_POST['year_id'],
															'subj_id' 			 => $_POST['subj_id'],
															'ps_id' 			 => $_POST['ps_id'],
															'section_id' 		 => $_POST['section_id']);
											$STUD->IrregularSetScheduleStudent($strArray, 'P', 'Y', true);
											break;
											
			case 'SendRequestIrregularSchedule'	: $STUD->SendRequestIrregularSchedule($_POST['stud_id']); break;
			
			case 'CancelRequestIrregularSchedule'	: $STUD->CancelRequestIrregularSchedule($_POST['stud_id']); break;
											
			//case 'IrregularDeleteSchedule'	: $STUD->IrregularDeleteScheduleStudent($_POST['stud_id'], $_POST['scsi_id'], $_POST['subj_group'], true); break;
			case 'IrregularDeleteSchedule'	: $STUD->IrregularDeleteScheduleStudent($_POST['stud_id'], $_POST['scsi_id'], $_POST['sem_linker'], $_POST['subj_group'], true); break;
			
			case 'SaveTimeSchedule'			: $STUD->SaveTimeSchedule($_POST['action_id'], $_POST['year_id']); break;
			
			case 'getTempLastSchedule'		: $getTempLastSchedule = $STUD->getTempLastSchedule($_POST['year_id']);
											  $tempLastSchedule = ( !empty($getTempLastSchedule) ) ? $getTempLastSchedule : array('sched_key' => 0, 'sched_to' => 0);
											  echo json_encode($tempLastSchedule); exit();
											  break;
			
			case 'getTempSchedule'		: $getTempSchedule  = $STUD->getTempSchedule($_POST['year_id']);
										  $tempSchedule 	= ( !empty($getTempSchedule) ) ? $getTempSchedule : 0;
										  echo json_encode($tempSchedule); exit();
										  break;
			
			case 'saveTempScheduleToLive'	  :  $STUD->saveTempScheduleToLive($_POST['year_id']); break;
			
			
			case 'deleteTempSchedule'	 : $deleteTempSchedule  = $STUD->deleteTempSchedule($_POST['trigger_id']); break;
			
			case 'SaveTempTimeSchedule'		: $insertTempTimeSchedule = $STUD->insertTempTimeSchedule($_POST['year_id'], $_POST['sched_from'], $_POST['sched_to'], $_POST['sched_key'], $_POST['sched_time']); 
											  echo json_encode($insertTempTimeSchedule); exit(); break;
			
			case 'SaveTimeIrregularSchedule' : 	$irreg_schedule = $_SESSION['irreg_schedule'];
			
												$timeYear = $STUD->getTimeSchedule('sched_time', $_POST['year_id']);
												
												if (!empty($irreg_schedule)) {
													$subjArr = array();
													foreach($irreg_schedule as $value) {
														
														$getSubject = $DSC->getSubject($value['subj_idFK']);
														
														if ( $subjArr[$value['subj_idFK']] < $getSubject['total_hrs'] ) {
														
															$subjArr[$value['subj_idFK']] += $timeYear[0];
															
															$strArray = array('stud_id' 			=> $value['stud_idFK'],
																			  'course_id' 			=> $value['course_idFK'],
																			  'sem_linker_id'		=> $value['sem_linker_idFK'],
																			  'year_id' 			=> $value['year_idFK'],
																			  'subj_id' 			=> $value['subj_idFK'],
																			  'prof_subj_sched_id' 	=> $value['prof_subj_sched_idFK']);
															$STUD->IrregularSetScheduleStudent($strArray);
														}
													}
													
													unset($_SESSION['irreg_schedule']);
												} break;	
			
			case 'ActivateNextSemesterSY' : $action = ( $_POST['sem_sem'] == 2 && $_POST['nssy'] > 0 ) ? 'Y' : 'N';
											$GLOBAL->activateNextSemesterSY($action); exit(); break;
			
		}
	}
	else {
		echo "<center>Connection Error: Please Refresh Your Page.</center>";
	}
	
?>